package cn.com.darly.videoandroidpm.ui.presenter;

import android.content.Context;
import android.view.View;

import com.darly.common.ToastApp;
import com.darly.common.retrofit.FrameListener;

import java.util.LinkedHashMap;
import java.util.Map;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseApplication;
import cn.com.darly.videoandroidpm.bean.BaseBean;
import cn.com.darly.videoandroidpm.bean.LoginInfo;
import cn.com.darly.videoandroidpm.common.EncryperAES;
import cn.com.darly.videoandroidpm.ui.RegisterNextActivity;
import cn.com.darly.videoandroidpm.ui.biz.RegisterNextListener;
import cn.com.darly.videoandroidpm.widget.shapeloading.ShapeLoadingDialog;


/**
 * @author zhangyh2 s 上午10:57:39 TODO 控制器 页面动作操作
 */
public class RegisterNextPresenter {

    private RegisterNextListener userBiz;
    private RegisterNextListener.RegisterNextInterface main;
    private ShapeLoadingDialog loading;

    public RegisterNextPresenter(RegisterNextListener.RegisterNextInterface main) {
        this.main = main;
        this.userBiz = new RegisterNextListener();
        loading = new ShapeLoadingDialog.Builder((RegisterNextActivity)main)
                .loadText("请求中...")
                .build();
    }


    /**
     * @param context
     * @param v       TODO点击事件在这里进行处理
     */
    public void onClickDown(Context context, View v) {
        switch (v.getId()) {
            case R.id.title_view_back_img:
                main.end();
                break;
            case R.id.title_view_ok:
                checkUser();
                break;
            default:
                break;
        }
    }


    public void checkUser() {
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("username", main.findAccount());
        userBiz.onStart(loading);
        main.setDisableClick();
        userBiz.onCheckUser(params, new FrameListener<BaseBean<String>>() {
            @Override
            public void onSucces(BaseBean<String> info) {
                userBiz.onStop(loading);
                main.setEnableClick();
                main.onCheckUserSuccess(info.getData());
            }

            @Override
            public void onFaild(int code, String info) {
                userBiz.onStop(loading);
                main.setEnableClick();
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onCheckUserFailed(info);
                }
            }
        });
    }

    public void register() {
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("deviceid", BaseApplication.getInstance().getDevId());
        params.put("tel", main.findTel());
        params.put("password", EncryperAES.Encode(main.findPwd()));
        params.put("username", main.findAccount());
        main.setDisableClick();
        userBiz.onRegister(params, new FrameListener<BaseBean<LoginInfo>>() {
            @Override
            public void onSucces(BaseBean<LoginInfo> info) {
                main.setEnableClick();
                main.onRegisterSuccess(info.getData());
            }

            @Override
            public void onFaild(int code, String info) {
                main.setEnableClick();
                if (code == 0){
                    ToastApp.showToast("服务端更新，请更新应用版本！");
                }else {
                    main.onRegisterFailed(info);
                }
            }
        });
    }
}